/**
 * @a https://leetcode.cn/problems/two-sum/
 */

#include "common.h"


class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> mp;
        for(int i = 0; i < nums.size(); ++i){
            int diff = target - nums[i];
            if(mp.count(diff)) return {i, mp[diff]};
            mp[nums[i]] = i;
        }
        return {-1, -1};    //make compiler happy
    }
};


class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> mp;
        int sz = nums.size();

        for(int i = 1; i < sz; ++i){
            int diff = target - nums[i];
            mp[nums[i - 1]] = i - 1;
            auto iter = mp.find(diff);
            if(iter == mp.end()) continue;
            return {i, iter->second};
        }
        return {-1,-1};     // 题目说明 不可能
    }
};